<?php
class App_Config
{
    private static $_instance = NULL;
    private $_strArray;
    private $_idsList;

    // 系统国家列表
    public static function optionsCountryList ()
    {
        $cityList = self::instance()->getArray('homepage_select_country');

        $ret      = array();
        foreach ($cityList as $id => $city)
        {
            $ret[$id] = $city;
        }

        return $ret;
    }
    public static function optionsCountryListNoId ()
    {
    	$cityList = self::instance()->getArray('homepage_select_country');
    
    	$ret      = array();
    	foreach ($cityList as $id => $city)
    	{
    		$ret[$city] = $city;
    	}
    
    	return $ret;
    }



    // 系统城市列表
    public static function optionsCityList ()
    {
        $cityList = self::instance()->getArray('homepage_select_city');

        $ret      = array();
        foreach ($cityList as $id => $city)
        {
            $ret[$id] = $city;
        }

        return $ret;
    }
    public static function optionsCityListNoId ()
    {
    	$cityList = self::instance()->getArray('homepage_select_city');
    
    	$ret      = array();
    	foreach ($cityList as $id => $city)
    	{
    		$ret[$city] = $city;
    	}
    
    	return $ret;
    }

    public static function optionsCityListByCountry ($country)
    {
    	$cityList = self::instance()->getStringByNameAndKey($country,'homepage_select_city');
    
    	$ret      = array();
    	foreach ($cityList as $id => $city)
    	{
    		$ret[$id] = $city;
    	}
    
    	return $ret;
    }
    // 系统景点类型列表
    public static function optionsScenicTypeList ()
    {
        $typeList = self::instance()->getArray('scenicspot_type');

        $ret      = array();
        foreach ($typeList as $id => $type)
        {
            $ret[$id] = $type;
        }

        return $ret;
    }
    public static function optionsScenicTypeListWithCity ()
    {
    	$query = DB::select('id','name','string')
    	->from('mod_config_array')
    	->where('key', '=', 'scenicspot_type')
    	->execute();
    	
    	$ret=array();
    	foreach ($query as $v)
    	{$ret[$v['id']]=$v['name'].'|'.$v['string'];}
    	
    	return $ret;
    
    	
    }
    public static function optionsScenicTypeListByCountry ($country)
    {
    	$query = DB::select('id','string')
    	->from('mod_config_array')
    	->where('key', '=', 'scenicspot_type')
    	->where('name', 'like', trim($country).'%')
    	->execute();
    	 
    	$ret=array();
    	foreach ($query as $v)
    	{
    		$ret[$v['id']]=$v['string'];
    	}
    	 
    	return $ret;
    
    }
    public static function optionsScenicTypeListByCity ($city)
    {
    	$query = DB::select('id','string')
    	->from('mod_config_array')
    	->where('key', '=', 'scenicspot_type')
    	->where('name', 'like', '%'.trim($city).'%')
    	->execute();
    
    	$ret=array();
    	foreach ($query as $v)
    	{
    		$ret[$v['id']]=$v['string'];
    	}
    
    	return $ret;
    
    }
    //线路类型
    public static function optionsLineCategory()
    {
    	$query = DB::select('id','type','name')
    	         ->from('tiyu_category')
    	         ->where('type','=','MAINCAT')
    	         ->execute();
    	$ret=array();
    	foreach($query as $v)
    	{
    		$ret[$v['id']]=$v['name'];   		
    	}
    	return $ret;
    }
    public static function getLabelString ($id, $type)
    {
        return self::instance()->getStringById($id, $type);
    }


    public static function instance()
    {
        if (empty(self::$_instance))
            self::$_instance = new App_Config;

        return self::$_instance;
    }
    
    public function __construct ()
    {
        $this->_strArray = array(); 
    }
    public function getStringById ($id, $type)
    {
        if (empty($this->_idsList))
        {
            $ret   = array();
            $query = DB::select()
                ->from('mod_config_string')
                ->execute();

            foreach ($query as $rs)
                $ret[ $rs['id'] ] = $rs['string'];

            $this->_idsList['string'] = $ret;

            $ret   = array();
            $query = DB::select()
                ->from('mod_config_array')
                ->execute();

            foreach ($query as $rs)
                $ret[ $rs['id'] ] = $rs['string'];

            $this->_idsList['array'] = $ret;
        }

        if ($type == 'array')
            return Arr::get($this->_idsList['array'], $id, '');
        else
            return Arr::get($this->_idsList['string'], $id, '');

    }

    public function getStrings()
    {
        if (empty($this->_strArray))
        {
            $query = DB::select()
                ->from('mod_config_string')
                ->execute();

            $ret = array();
            foreach ($query as $rs)
            {
                $ret[$rs['key']] = $rs['string'];
            }

            $this->_strArray = $ret;
        }

        return $this->_strArray;
    }

    public function getArrays ()
    {
        if (empty($this->_arrList))
        {
            $query = DB::select()
                ->from('mod_config_array')
                ->execute();

            $ret = array();
            foreach ($query as $rs)
            {
                if (! isset($ret[$rs['key']]))
                    $ret[$rs['key']] = array();
                $ret[$rs['key']][$rs['id']] = $rs['string'];
            }

            $this->_arrList = $ret;
        }

        return $this->_arrList;
 
    }

    public function getString($key)
    {
        if (empty($this->_strArray))
        {
            $query = DB::select()
                ->from('mod_config_string')
                ->execute();

            $ret = array();
            foreach ($query as $rs)
            {
                $ret[$rs['key']] = $rs['string'];
            }

            $this->_strArray = $ret;
        }

        return Arr::get($this->_strArray, $key, '');
    }

    public function getArray ($key)
    {
        if (empty($this->_arrList))
        {
            $this->getArrays();
        }

        return Arr::get($this->_arrList, $key, array());
    }

   public function getStringByNameAndKey($name,$key)
    {
    	$query = DB::select('id','string')
    	->from('mod_config_array')
    	->where('key', '=', $key)
    	->where('name','=',$name)
    	->execute();
    	$str=array();
    	foreach ($query as $v)
    	{$str[$v['id']]=$v['string'];}
    
    	return $str;
    } 
}
